package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.SearchAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.entity.HotSongs;
import com.example.myapplication.entity.Search;
import com.example.myapplication.tools.BarTransparent;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchActivity extends AppCompatActivity {
    private RecyclerView search_recycleView;
    private SearchAdapter adapter;
    private List<Search.ResultBean.SongsBean> hotSongsList;
    private Context context;
    private ImageView song_img;
    private TextView name_song;
    private View item;
    private Search.ResultBean.SongsBean songsBean ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        BarTransparent.makeStatusBarTransparent(this);
        BarTransparent.setAndroidNativeLightStatusBar(this,true);
        context=this;
        item = findViewById(R.id.item);
        name_song = findViewById(R.id.name_song);
        song_img = findViewById(R.id.song_img);
        search_recycleView = findViewById(R.id.search_recycleView);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        search_recycleView.setLayoutManager(manager);
        adapter = new SearchAdapter();
        search_recycleView.setAdapter(adapter);
        Intent intent = getIntent();
        String query = intent.getStringExtra("query");

        loadData(query);
        TextView t1 = findViewById(R.id.t1);
        t1.setText("为你推荐:"+query+"单曲");
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(SearchActivity.this,SingerDetailActivity.class);
                Artist artist = new Artist();
                artist.setId(songsBean.getAr().get(0).getId());
                artist.setName(songsBean.getAr().get(0).getName());
                artist.setImg1v1Url(songsBean.getAl().getPicUrl());
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("singer",artist);
                intent2.putExtras(bundle1);
                startActivity(intent2);
            }
        });

        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void onclick(int position) {
               Search.ResultBean.SongsBean songsBean =  hotSongsList.get(position);
               HotSongs hotSongs = new HotSongs();

               hotSongs.setName(songsBean.getName());
               hotSongs.setId(songsBean.getId());

                HotSongs.AlBean alBean = new HotSongs.AlBean();
                alBean.setName(songsBean.getAl().getName());
                alBean.setPicUrl(songsBean.getAl().getPicUrl());
                hotSongs.setAl(alBean);

                HotSongs.ArBean arBean = new HotSongs.ArBean();
                arBean.setName(songsBean.getAr().get(0).getName());
                List<HotSongs.ArBean> arBeanList = new ArrayList<>();
                arBeanList.add(arBean);
                hotSongs.setAr(arBeanList);

               Intent intent1 = new Intent(context, SongDetail1Activity.class);

               Bundle bundle = new Bundle();
               bundle.putSerializable("song",hotSongs);
               intent1.putExtras(bundle);
               startActivity(intent1);

            }
        });



    }
    private void loadData(String query) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<Search> call = service.getSearch(query);
        call.enqueue(new Callback<Search>() {
            @Override
            public void onResponse(Call<Search> call, Response<Search> response) {
                Search baseResponse = response.body();
                hotSongsList = baseResponse.getResult().getSongs();
                adapter.setData(hotSongsList);
                songsBean = hotSongsList.get(0);
                RoundedCorners roundedCorners= new RoundedCorners(30);
                RequestOptions options1= RequestOptions
                        .bitmapTransform(roundedCorners)
                        .circleCrop();
                Glide.with(context).load(songsBean.getAl().getPicUrl())
                        .centerCrop()
                        .error(R.drawable.ic_baseline_refresh_24)
                        .placeholder(R.drawable.ic_baseline_refresh_24)
                        .apply(options1)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(song_img);
                name_song.setText("歌手："+songsBean.getAr().get(0).getName());
            }
            @Override
            public void onFailure(Call<Search> call, Throwable t) {

                t.printStackTrace();
            }
        });
    }
}